﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mock1G3.BaseEntities;
using Mock1G3.BussinessLogic;

namespace Mock1G3.UserControl.UCValidateCard
{
    public partial class ValidateCardNo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnCheck_OnClick(object sender, EventArgs e)
        {
            if (txtCardNo.Visible == true)
            {
                if (txtCardNo.Text == "")
                {
                    Label2.Text = "Numeric Card is not empty".ToString();
                }
                else
                {
                    Card card = new Card();
                    card.CardNo = txtCardNo.Text;
                    Session.Add("CardNo", txtCardNo.Text);

                    if (card.CardNo.Length == 16 & ValidateLogic.CheckCardNo(card) == true)
                    {
                        if (BussinessLogic.ValidateLogic.CheckStatus(card) == true)
                        {
                            if (BussinessLogic.ValidateLogic.CheckExpiredDate(card) == true)
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(2));
                                Label2.Text = "Card have supported! Please enter your PIN!";
                                txtPIN.Visible = true;
                                txtCardNo.Visible = false;
                                Session.Add("CardNo", txtCardNo.Text);
                                if (BussinessLogic.ValidateLogic.CheckAttempt(card) == 2)
                                {
                                    Thread.Sleep(TimeSpan.FromSeconds(2));
                                    Label2.Text = "You have entered 2 times! Enter the PIN last time!";
                                }
                            }
                            else
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(2));
                                Label2.Text = "Card is expired. Please contact our Bank!\nEject card...";
                            }
                        }
                        else
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(2));
                            Label2.Text = "Card is blocked!\nEject card...";
                        }
                    }
                    else
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                        Label2.Text = "Card is not supported!\nEject card...";
                        Session.Add("CardNo", "");
                    }
                }
            }
            else
            {
                if (txtPIN.Text == "")
                {
                    Label2.Text = "Numeric PIN is not empty".ToString();
                }
                else
                {
                    int valueAttempt;
                    Card card = new Card();
                    card.CardNo = Session["CardNo"].ToString();
                    string txtPin = txtPIN.Text;
                    card.PIN = GetMD5(txtPin);

                    if (txtPin.Length == 6 & BussinessLogic.ValidateLogic.CheckCard(card) == true)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                        card.Attempt = 0;
                        BussinessLogic.ValidateLogic.UpdateAttempt(card);
                        Label2.Text = "The PIN is valid! Login success!";
                        txtPIN.Enabled = false;
                        DataTable dt = BussinessLogic.ValidateLogic.Table(card);
                        DataRow dr = dt.Rows[0];
                        Session.Add("PIN",txtPin);
                        Session.Add("ATMID", 1);
                        Session.Add("CustID", dr["CustID"]);
                        Session.Add("Name", dr["Name"]);
                        Session.Add("AccountID", dr["AccountID"]);
                        Session.Add("AccountNo", dr["AccountNo"]);
                        Label2.Text = Session["Name"].ToString();
                        Response.Redirect("Default.aspx");
                    }
                    else
                    {
                        valueAttempt = BussinessLogic.ValidateLogic.CheckAttempt(card);
                        if (valueAttempt < 1)
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(2));
                            valueAttempt++;
                            card.Attempt = valueAttempt;
                            BussinessLogic.ValidateLogic.UpdateAttempt(card);
                            Label2.Text = "PIN is invalid! Please re-enter the PIN!";
                        }
                        else
                        {
                            if (BussinessLogic.ValidateLogic.CheckAttempt(card) == 1)
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(2));
                                Label2.Text = "You have entered 2 times! Enter the PIN last time!";
                                valueAttempt++;
                                card.Attempt = valueAttempt;
                                BussinessLogic.ValidateLogic.UpdateAttempt(card);
                            }
                            else
                            {
                                Thread.Sleep(TimeSpan.FromSeconds(2));
                                valueAttempt++;
                                card.Attempt = valueAttempt;
                                BussinessLogic.ValidateLogic.UpdateAttempt(card);
                                txtPIN.Enabled = false;
                                Label2.Text = "You have entered 3 times! Card has been Blocked\nSwallow card...";
                                Session.RemoveAll();
                                //Response.Redirect("Default.aspx");
                            }
                        }
                    }
                }
            }
        }

        protected void btnClear_OnClick(object sender, EventArgs e)
        {
            if (txtCardNo.Visible == true)
            {
                txtCardNo.Text = "";
                txtCardNo.Focus();
            }
            else
            {
                txtPIN.Text = "";
                txtPIN.Focus();
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Response.Redirect("Default.aspx");
        }

        public static string GetMD5(string str)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));

            StringBuilder sbHash = new StringBuilder();

            foreach (byte b in bHash)
            {
                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();

        }   

    }
}